from django.http import JsonResponse

try:
    from django.utils.deprecation import MiddlewareMixin  # Django 1.10.x
except ImportError:
    MiddlewareMixin = object
# 白名单，表示请求里面的路由时不验证登录信息
API_WHITELIST = ["/patient/escort_certification/", "/patient/register_login/", "/patient/msg/", "/patient/sms/", ]


class AuthorizeMiddleware(MiddlewareMixin):
    def process_request(self, request):
        print('path?', request.path)
        print(request)
        if request.path not in API_WHITELIST:
            # 从请求头中获取 token
            token = request.META.get('HTTP_AUTHORIZATION')
            if token is None:
                return JsonResponse({'error': 100001, 'msg': "未查询到登录信息"}, status=401)
#
# from django.http import JsonResponse
# try:
#     from django.utils.deprecation import MiddlewareMixin  # Django 1.10.x
# except ImportError:
#     MiddlewareMixin = object
# # 白名单，表示请求里面的路由时不验证登录信息
# API_WHITELIST = ["/patient/register_login/", "/patient/msg/", "/patient/sms/"]
#
# class AuthorizeMiddleware(MiddlewareMixin):
#     def process_request(self, request):
#         print('path?', request.path)
# API_WHITELIST = ["/patient/register_login/", "/patient/msg/","/patient/sms/"]
#
# class AuthorizeMiddleware(MiddlewareMixin):
#     def process_request(self, request):
#         print('path?',request.path)
#         if request.path not in API_WHITELIST:
#             # 从请求头中获取 token
#             token = request.META.get('HTTP_AUTHORIZATION')
#             if token is None:
#                 return JsonResponse({'error': 100001, 'msg': "未查询到登录信息"}, status=401)

